home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zarith.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  338 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zarith.c */
  20. /* Arithmetic operators for Ghostscript */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /****** NOTE: none of the arithmetic operators  ******/
  28. /****** currently check for floating exceptions ******/
  29.  
  30. /* Imported operators */
  31. extern int zcvi(P1(os_ptr));
  32.  
  33. /* Define max and min values for what will fit in value.intval. */
  34. #define min_intval min_long
  35. #define max_intval max_long
  36. #define max_half_intval ((1 << (size_of(long) / 2 - 1)) - 1)
  37.  
  38. /* Macro for accessing next-to-top stack element */
  39. #define opm1 (op-1)
  40. /* Macros for generating non-integer cases for arithmetic operations. */
  41. /* 'frob' is one of the arithmetic operators, +, -, or *. */
  42. #define non_int_cases(frob,frob_equals)\
  43.  switch ( r_type(op) ) {\
  44.   default: return_error(e_typecheck);\
  45.   case t_real: switch ( r_type(opm1) ) {\
  46.    default: return_error(e_typecheck);\
  47.    case t_real: op[-1].value.realval frob_equals op->value.realval; break;\
  48.    case t_integer: make_real(opm1, op[-1].value.intval frob op->value.realval);\
  49.   } break;\
  50.   case t_integer: switch ( r_type(opm1) ) {\
  51.    default: return_error(e_typecheck);\
  52.    case t_real: op[-1].value.realval frob_equals op->value.intval; break;\
  53.    case t_integer:
  54. #define end_cases()\
  55.   } }
  56.  
  57. /* <num1> <num2> add <sum> */
  58. /* We make this into a separate procedure because */
  59. /* the interpreter will almost always call it directly. */
  60. int
  61. zop_add(register os_ptr op)
  62. {    non_int_cases(+, +=)
  63.        {    long int2 = op->value.intval;
  64.         if ( ((op[-1].value.intval += int2) ^ int2) < 0 &&
  65.              ((op[-1].value.intval - int2) ^ int2) >= 0
  66.            )
  67.            {    /* Overflow, convert to real */
  68.             make_real(opm1, (float)(op[-1].value.intval - int2) + int2);
  69.            }
  70.        }
  71.     end_cases()
  72.     return 0;
  73. }
  74. int
  75. zadd(os_ptr op)
  76. {    int code = zop_add(op);
  77.     if ( code == 0 ) { pop(1); }
  78.     return code;
  79. }
  80.  
  81. /* <num1> <num2> div <real_quotient> */
  82. int
  83. zdiv(register os_ptr op)
  84. {    register os_ptr op1 = op - 1;
  85.     /* We can't use the non_int_cases macro, */
  86.     /* because we have to check explicitly for op == 0. */
  87.     switch ( r_type(op) )
  88.        {
  89.     default:
  90.         return_error(e_typecheck);
  91.     case t_real:
  92.         if ( op->value.realval == 0 )
  93.             return_error(e_undefinedresult);
  94.         switch ( r_type(op1) )
  95.            {
  96.         default:
  97.             return_error(e_typecheck);
  98.         case t_real:
  99.             op1->value.realval /= op->value.realval;
  100.             break;
  101.         case t_integer:
  102.             make_real(op1, op1->value.intval / op->value.realval);
  103.            }
  104.         break;
  105.     case t_integer:
  106.         if ( op->value.intval == 0 )
  107.             return_error(e_undefinedresult);
  108.         switch ( r_type(op1) )
  109.            {
  110.         default:
  111.             return_error(e_typecheck);
  112.         case t_real:
  113.             op1->value.realval /= op->value.intval; break;
  114.         case t_integer:
  115.             make_real(op1, (float)op1->value.intval / op->value.intval);
  116.            }
  117.        }
  118.     pop(1);
  119.     return 0;
  120. }
  121.  
  122. /* <num1> <num2> mul <product> */
  123. int
  124. zmul(register os_ptr op)
  125. {    non_int_cases(*, *=)
  126.        {    long int1 = op[-1].value.intval;
  127.         long int2 = op->value.intval;
  128.         long abs1 = (int1 >= 0 ? int1 : - int1);
  129.         long abs2 = (int2 >= 0 ? int2 : - int2);
  130.         float fprod;
  131.         if (    (abs1 > max_half_intval || abs2 > max_half_intval) &&
  132.             /* At least one of the operands is very large. */
  133.             /* Check for integer overflow. */
  134.             abs1 != 0 &&
  135.             abs2 > max_intval / abs1 &&
  136.             /* Check for the boundary case */
  137.             (fprod = (float)int1 * int2,
  138.              (int1 * int2 != min_intval ||
  139.              fprod != (float)min_intval))
  140.            )
  141.             make_real(opm1, fprod);
  142.         else
  143.             op[-1].value.intval = int1 * int2;
  144.        }
  145.     end_cases()
  146.     pop(1);
  147.     return 0;
  148. }
  149.  
  150. /* <num1> <num2> sub <difference> */
  151. /* We make this into a separate procedure because */
  152. /* the interpreter will almost always call it directly. */
  153. int
  154. zop_sub(register os_ptr op)
  155. {    non_int_cases(-, -=)
  156.        {    long int1 = op[-1].value.intval;
  157.         if ( (int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
  158.              (int1 ^ op->value.intval) < 0
  159.            )
  160.            {    /* Overflow, convert to real */
  161.             make_real(opm1, (float)int1 - op->value.intval);
  162.            }
  163.        }
  164.     end_cases()
  165.     return 0;
  166. }
  167. int
  168. zsub(os_ptr op)
  169. {    int code = zop_sub(op);
  170.     if ( code == 0 ) { pop(1); }
  171.     return code;
  172. }
  173.  
  174. /* <num1> <num2> idiv <int_quotient> */
  175. int
  176. zidiv(register os_ptr op)
  177. {    /* The Red Book says this only works on integers, */
  178.     /* but implementations also accept reals. */
  179.     /* However, it must be implemented correctly for integers! */
  180.     register os_ptr op1 = op - 1;
  181.     float quo;
  182.     /* We can't use the non_int_cases macro, */
  183.     /* because we have to check explicitly for op == 0. */
  184.     switch ( r_type(op) )
  185.        {
  186.     default:
  187.         return_error(e_typecheck);
  188.     case t_real:
  189.         if ( op->value.realval == 0 )
  190.             return_error(e_undefinedresult);
  191.         switch ( r_type(op1) )
  192.            {
  193.         default:
  194.             return_error(e_typecheck);
  195.         case t_real:
  196.             quo = op1->value.realval / op->value.realval;
  197.             break;
  198.         case t_integer:
  199.             quo = op1->value.intval / op->value.realval;
  200.             break;
  201.            }
  202.         break;
  203.     case t_integer:
  204.         if ( op->value.intval == 0 )
  205.             return_error(e_undefinedresult);
  206.         switch ( r_type(op1) )
  207.            {
  208.         default:
  209.             return_error(e_typecheck);
  210.         case t_real:
  211.             quo = op1->value.realval / op->value.intval;
  212.             break;
  213.         case t_integer:
  214.             if ( (op1->value.intval /= op->value.intval) ==
  215.                 min_intval && op->value.intval == -1
  216.                )
  217.                {    /* Anomalous boundary case, fail. */
  218.                 return_error(e_rangecheck);
  219.                }
  220.             pop(1);
  221.             return 0;
  222.            }
  223.         break;
  224.        }
  225.     if ( !zcvi_possible(quo) )
  226.         return_error(e_rangecheck);
  227.     make_int(op1, (long)quo);
  228.     pop(1);
  229.     return 0;
  230. }
  231.  
  232. /* <int1> <int2> mod <remainder> */
  233. int
  234. zmod(register os_ptr op)
  235. {    check_type(op[-1], t_integer);
  236.     check_type(*op, t_integer);
  237.     if ( op->value.intval == 0 )
  238.         return_error(e_undefinedresult);
  239.     op[-1].value.intval %= op->value.intval;
  240.     pop(1);
  241.     return 0;
  242. }
  243.  
  244. /* <num1> neg <num2> */
  245. int
  246. zneg(register os_ptr op)
  247. {    switch ( r_type(op) )
  248.        {
  249.     default:
  250.         return_error(e_typecheck);
  251.     case t_real:
  252.         op->value.realval = -op->value.realval;
  253.         break;
  254.     case t_integer:
  255.         if ( op->value.intval == min_intval )
  256.             make_real(op, -(float)min_intval);
  257.         else
  258.             op->value.intval = -op->value.intval;
  259.        }
  260.     return 0;
  261. }
  262.  
  263. /* <num1> ceiling <num2> */
  264. int
  265. zceiling(register os_ptr op)
  266. {    switch ( r_type(op) )
  267.        {
  268.     default:
  269.         return_error(e_typecheck);
  270.     case t_real:
  271.         op->value.realval = ceil(op->value.realval);
  272.     case t_integer: ;
  273.        }
  274.     return 0;
  275. }
  276.  
  277. /* <num1> floor <num2> */
  278. int
  279. zfloor(register os_ptr op)
  280. {    switch ( r_type(op) )
  281.        {
  282.     default:
  283.         return_error(e_typecheck);
  284.     case t_real:
  285.         op->value.realval = floor(op->value.realval);
  286.     case t_integer: ;
  287.        }
  288.     return 0;
  289. }
  290.  
  291. /* <num1> round <num2> */
  292. int
  293. zround(register os_ptr op)
  294. {    switch ( r_type(op) )
  295.        {
  296.     default:
  297.         return_error(e_typecheck);
  298.     case t_real:
  299.         op->value.realval = floor(op->value.realval + 0.5);
  300.     case t_integer: ;
  301.        }
  302.     return 0;
  303. }
  304.  
  305. /* <num1> truncate <num2> */
  306. int
  307. ztruncate(register os_ptr op)
  308. {    switch ( r_type(op) )
  309.        {
  310.     default:
  311.         return_error(e_typecheck);
  312.     case t_real:
  313.         op->value.realval =
  314.             (op->value.realval < 0.0 ?
  315.                 ceil(op->value.realval) :
  316.                 floor(op->value.realval));
  317.     case t_integer: ;
  318.        }
  319.     return 0;
  320. }
  321.  
  322. /* ------ Initialization table ------ */
  323.  
  324. op_def zarith_op_defs[] = {
  325.     {"2add", zadd},
  326.     {"1ceiling", zceiling},
  327.     {"2div", zdiv},
  328.     {"2idiv", zidiv},
  329.     {"1floor", zfloor},
  330.     {"2mod", zmod},
  331.     {"2mul", zmul},
  332.     {"1neg", zneg},
  333.     {"1round", zround},
  334.     {"2sub", zsub},
  335.     {"1truncate", ztruncate},
  336.     op_def_end(0)
  337. };
  338.